package com.sc.ifs.member;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sc.dal.model.member.Captcha;
import com.sc.dal.model.member.Member;
import com.sc.service.member.CaptchaService;
import com.sc.service.member.MemberService;
import com.sc.util.data.Common;
import com.sc.util.data.DataUtil;
import com.sc.util.data.SendMessage;
import com.sc.util.exception.ExceptionEnum;
import com.sc.util.sms.SmsVerificationCode;

/**
 * 
 * <h1>app短信验证码</h1> 
 * 
 * @author xujunbao 
 * @version CaptchaController.java,v0.1 2017年3月3日 下午5:07:34 xujunbao Exp  
 *
 */
@Controller
@RequestMapping("/app/captcha")
public class AppCaptchaController {
	/**引入验证码的业务逻辑层*/
	@Autowired
	private CaptchaService captchaService;
	/**引入用户的业务逻辑层*/
	@Autowired
	private MemberService memberService;
	@Autowired
	private EhCacheCacheManager factory;
	/**
	 * 
	 * <h2>发送短信验证码</h2> 
	 * 
	 * @param mPhone 手机号
	 * @param mStyle 验证码的用途
	 * @return
	 * @return String 信息发送状态 0:用户已存在，1:短信发送失败,2:发送成功,3:用户未注册
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/getCaptcha", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getCaptcha(String mPhone,String mStyle){
		Map<String, Object> resultMap=new HashMap<String, Object>();
		Member member=memberService.selectByPhone(mPhone);
		Captcha captcha=new Captcha();
		if(Common.isEmpty(mStyle)){
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_PARAM);
			return resultMap;
		}
		/*** 注册新用户 ************/
		if(mStyle!=null && "1".equals(mStyle)){
			if(member!=null){
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_USER_EXIST);
				return resultMap;
			}else{
				//获取一个6位数字的验证码
				String code = Common.getvalidcode();
				/***调用短信接口，发送短信验证码***********/
				Map map = SmsVerificationCode.sendVerificationCode(mPhone,code);
				//返回状态值：成功返回Success 失败返回：Faild
				String returnstatus = map.get("returnstatus").toString();
				//相关的错误描述
				String message = map.get("message").toString();
				if(returnstatus!=null && "Success".equals(returnstatus)){
					//System.out.println("短信发送成功！！！！"+message);
					/***短信发送成功，将验证码缓存，注册时进行校验！！！*************************/
					Cache levelOneCache = factory.getCache("smsVerificationCode");
					levelOneCache.put(mPhone, code);
					/**记录发送短信**************/
					captcha.setPhone(mPhone);
					captcha.setStyle(mStyle);
					captcha.setContent(code);
					captcha.setCreatedate(new Date().getTime());
					captchaService.insertSelective(captcha);
					DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
					return resultMap;
				}else{
					System.out.println("短信发送失败！！！！"+message);
					DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SEND_EXIST);
					return resultMap;
				}
			}
			/*** 忘记密码     发送验证码************/
		}else if(mStyle!=null && "2".equals(mStyle)){
			/**判断当前用户是否存在，如不存在，说明该用户未注册****/
			if(member!=null){
				//获取一个6位数字的验证码
				String code = Common.getvalidcode();
				/***调用短信接口，发送短信验证码***********/
				Map map = SmsVerificationCode.sendVerificationCode(mPhone,code);
				//返回状态值：成功返回Success 失败返回：Faild
				String returnstatus = map.get("returnstatus").toString();
				//相关的错误描述
				String message = map.get("message").toString();
				if(returnstatus!=null && "Success".equals(returnstatus)){
					//System.out.println("短信发送成功！！！！"+message);
					/***短信发送成功，将验证码进行缓存，注册时进行校验！！！*************************/
					Cache levelOneCache = factory.getCache("smsVerificationCode");
					levelOneCache.put(mPhone, code);
					/**记录发送短信**************/
					captcha.setPhone(mPhone);
					captcha.setStyle(mStyle);
					captcha.setContent(code);
					captcha.setCreatedate(new Date().getTime());
					captchaService.insertSelective(captcha);
					DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
					return resultMap;
				}else{
					//System.out.println("短信发送失败！！！！"+message);
					DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SEND_EXIST);
					return resultMap;
				}
			}else{
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_USER_NOT_REGISTER);
				return resultMap;
			}
		}
		return resultMap;
	}
}
